package me.zhengjie.controller;


import me.zhengjie.annotation.Log;
import me.zhengjie.dto.sqlServer.ProductionInfo;
import me.zhengjie.service.ProductionInfoService;
import me.zhengjie.service.dto.ProductionInfoDto;
import me.zhengjie.service.dto.ProductionInfoQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;


@RestController
@RequiredArgsConstructor
@Api(tags = "生产信息管理")
@RequestMapping("/api/productionInfo")
public class ProductionInfoController {

    private final ProductionInfoService productionInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('productionInfo:list')")
    public void exportProductionInfo(HttpServletResponse response, ProductionInfoQueryCriteria criteria) throws IOException {
        productionInfoService.download(productionInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询生产信息")
    @ApiOperation("查询生产信息")
    @PreAuthorize("@el.check('productionInfo:list')")
    public ResponseEntity<PageResult<ProductionInfoDto>> queryProductionInfo(ProductionInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(productionInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增生产信息")
    @ApiOperation("新增生产信息")
    @PreAuthorize("@el.check('productionInfo:add')")
    public ResponseEntity<Object> createProductionInfo(@Validated @RequestBody ProductionInfo resources){
        productionInfoService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改生产信息")
    @ApiOperation("修改生产信息")
    @PreAuthorize("@el.check('productionInfo:edit')")
    public ResponseEntity<Object> updateProductionInfo(@Validated @RequestBody ProductionInfo resources){
        productionInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除生产信息")
    @ApiOperation("删除生产信息")
    @PreAuthorize("@el.check('productionInfo:del')")
    public ResponseEntity<Object> deleteProductionInfo(@RequestBody Integer[] ids) {
        productionInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
